package com.unicom.office.func;

import com.unicom.office.dataitem.ListDataItem;
import com.unicom.office.dataitem.NumberDataItem;
import com.unicom.office.runtime.OfficeRuntime;
import com.unicom.office.type.ListTypeItem;
import com.unicom.office.type.TypeItem;
import com.unicom.office.unit.OfficeUnit;

public class ListItemFunc extends FuncBase {
    private FuncBase listFunc;
    private Object indexObj;

    public void setListFunc(FuncBase listFunc) {
        this.listFunc = listFunc;
    }

    public void setIndexObj(Object indexObj) {
        this.indexObj = indexObj;
    }

    public Object run(OfficeRuntime officeRuntime) {
        Integer index = null;
        if(indexObj instanceof NumberDataItem){
            index = ((NumberDataItem)indexObj).getIntValue();
        }else if(indexObj instanceof FuncBase){
            NumberDataItem runned = (NumberDataItem)((FuncBase) indexObj).run(officeRuntime);
            index = runned.getIntValue();
        }
        ListDataItem list = (ListDataItem) listFunc.run(officeRuntime);
        return list.get(index);
    }

    public void runForMatch(OfficeUnit unit, OfficeRuntime officeRuntime) {

    }

    public TypeItem getType() {
        ListTypeItem type = (ListTypeItem)listFunc.getType();
        return type.getItemType();
    }
}
